import { createSlice } from "@reduxjs/toolkit";
import * as c from "@/util/c";
import { log } from "@/util/tio";

const initialState = {
  curr: null, //{ name: "tan", id: "4444", phone: "18677778888" },
  theme: c.DEFAULT_THEME, //'light', //当前主题 dark or light, default is light
  tws: null, //TioWebsocket
};
export const rootSlice = createSlice({
  name: "root",
  initialState,
  reducers: {
    upState(state, action) {
      // 错误用法，
      // state = { ...state ,... action.payload };
      log(state, action)
      state = Object.assign(state, action.payload);
      // state.theme = action.payload.theme;
      // console.log("state", state);
    },
  },
});

/**
 * 
 * @param state 
 * @returns 
 */
export const selectRoot = (state) => state.root;
export const { upState } = rootSlice.actions;

/**
 * 
 * @param root 
 * @param dispatch 
 */
export const update = (root, dispatch) => {
  dispatch(upState(root));
}
export default rootSlice.reducer;
